<!DOCTYPE html>
<html lang="zh-CN" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>条码打印系统</title>
    <style>
        body {
            font-family: 'Microsoft YaHei', Arial, sans-serif;
            margin: 0;
            padding: 20px;
            background-color: #f5f5f5;
        }
        .container {
            max-width: 800px;
            margin: 0 auto;
            background-color: white;
            padding: 30px;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
        }
        h1 {
            text-align: center;
            color: #333;
            margin-bottom: 30px;
        }
        .form-group {
            margin-bottom: 20px;
        }
        label {
            display: block;
            margin-bottom: 5px;
            font-weight: bold;
            color: #555;
        }
        input[type="text"], input[type="number"], input[type="date"] {
            width: 100%;
            padding: 10px;
            border: 1px solid #ddd;
            border-radius: 4px;
            font-size: 14px;
            box-sizing: border-box;
        }
        input[type="text"]:focus, input[type="number"]:focus, input[type="date"]:focus {
            border-color: #007bff;
            outline: none;
            box-shadow: 0 0 5px rgba(0,123,255,0.3);
        }
        .btn {
            background-color: #007bff;
            color: white;
            padding: 12px 30px;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            font-size: 16px;
            width: 100%;
            margin-top: 10px;
        }
        .btn:hover {
            background-color: #0056b3;
        }
        .btn:disabled {
            background-color: #6c757d;
            cursor: not-allowed;
        }
        .result {
            margin-top: 20px;
            padding: 15px;
            border-radius: 4px;
            display: none;
        }
        .result.success {
            background-color: #d4edda;
            border: 1px solid #c3e6cb;
            color: #155724;
        }
        .result.error {
            background-color: #f8d7da;
            border: 1px solid #f5c6cb;
            color: #721c24;
        }
        .result.warning {
            background-color: #fff3cd;
            border: 1px solid #ffeaa7;
            color: #856404;
        }
        .loading {
            display: none;
            text-align: center;
            margin-top: 10px;
        }
        .spinner {
            border: 4px solid #f3f3f3;
            border-top: 4px solid #007bff;
            border-radius: 50%;
            width: 30px;
            height: 30px;
            animation: spin 1s linear infinite;
            margin: 0 auto;
        }
        @keyframes spin {
            0% { transform: rotate(0deg); }
            100% { transform: rotate(360deg); }
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="navigation" style="text-align: left; margin-bottom: 20px;">
            <a href="/" style="color: #007bff; text-decoration: none; font-size: 14px;">← 返回主菜单</a>
        </div>
        <h1>条码打印系统</h1>
        <form id="printForm">
            <div class="form-group">
                <label for="productionDate">生产日期:</label>
                <input type="date" id="productionDate" name="productionDate" required>
            </div>
            
            <div class="form-group">
                <label for="productionBatch">生产批次:</label>
                <input type="text" id="productionBatch" name="productionBatch" required placeholder="请输入生产批次">
            </div>
            
            <div class="form-group">
                <label for="startSerial">开始序号:</label>
                <input type="number" id="startSerial" name="startSerial" min="1" required placeholder="请输入开始序号">
            </div>
            
            <div class="form-group">
                <label for="endSerial">结束序号:</label>
                <input type="number" id="endSerial" name="endSerial" min="1" required placeholder="请输入结束序号">
            </div>
            
            <button type="submit" class="btn" id="printBtn">开始打印</button>
        </form>
        
        <div class="loading" id="loading">
            <div class="spinner"></div>
            <p>正在打印中，请稍候...</p>
        </div>
        
        <div class="result" id="result">
            <div id="resultMessage"></div>
        </div>
    </div>

    <script>
        document.getElementById('printForm').addEventListener('submit', function(e) {
            e.preventDefault();
            
            const formData = new FormData(this);
            const printBtn = document.getElementById('printBtn');
            const loading = document.getElementById('loading');
            const result = document.getElementById('result');
            const resultMessage = document.getElementById('resultMessage');
            
            // 验证表单
            const startSerial = parseInt(formData.get('startSerial'));
            const endSerial = parseInt(formData.get('endSerial'));
            
            if (startSerial > endSerial) {
                showResult('error', '开始序号不能大于结束序号');
                return;
            }
            
            if (startSerial < 1 || endSerial < 1) {
                showResult('error', '序号必须大于0');
                return;
            }
            
            // 显示加载状态
            printBtn.disabled = true;
            loading.style.display = 'block';
            result.style.display = 'none';
            
            // 发送打印请求
            fetch('/api/print', {
                method: 'POST',
                body: formData
            })
            .then(response => response.json())
            .then(data => {
                if (data.success) {
                    showResult('success', `${data.message}<br>成功打印: ${data.data.printCount}/${data.data.totalCount} 个标签`);
                } else {
                    if (data.code === 206) {
                        showResult('warning', `${data.message}<br>成功打印: ${data.data.printCount}/${data.data.totalCount} 个标签`);
                    } else {
                        showResult('error', data.message);
                    }
                }
            })
            .catch(error => {
                console.error('Error:', error);
                showResult('error', '请求失败，请检查网络连接');
            })
            .finally(() => {
                printBtn.disabled = false;
                loading.style.display = 'none';
            });
        });
        
        function showResult(type, message) {
            const result = document.getElementById('result');
            const resultMessage = document.getElementById('resultMessage');
            
            result.className = 'result ' + type;
            resultMessage.innerHTML = message;
            result.style.display = 'block';
        }
        
        // 设置默认日期为今天
        document.getElementById('productionDate').valueAsDate = new Date();
    </script>
</body>
</html>